class MyImg extends eui.Image
{
	//背景框的宽高
	public containerWidth:number;
	public containerHeight:number;
	//原始图片的宽高
	public originalWidth:number;
	public originalHeight:number;
	//图片第一次自适应的宽高
	public resizeWidth:number;
	public resizeHeight:number;
	//图片最大的缩放值
	public maxScale:number=2;

	public constructor(containerWidth:number,containerHeight:number) 
	{
		super();
		this.containerWidth=containerWidth;
		this.containerHeight=containerHeight;
		this.smoothing=true;
	}

	//导入图片的时候调整图片大小位置
	public startResize(originalWidth:number,originalHeight:number):void
	{
		this.scaleX=1;
		this.scaleY=1;
		//将图片缩小
		this.originalWidth=originalWidth;
		this.originalHeight=originalHeight;
		this.width=originalWidth;
		this.height=originalHeight;
		var wRate:number=this.containerWidth/this.width;
		var hRate:number=this.containerHeight/this.height;
        if(wRate>hRate){
			this.width=this.containerWidth;
			this.height=this.height*wRate;
		}
		else{
			this.height=this.containerHeight;
			this.width=this.width*hRate;
		}
		this.resizeWidth=this.width;
		this.resizeHeight=this.height;
        this.anchorOffsetX = this.width/2;
        this.anchorOffsetY = this.height/2;
		this.x = this.containerWidth/2;
        this.y = this.containerHeight/2;
	}
	//截取图片的时候重新调整图片
	public cutResize():void
	{
		var centerX:number=this.x;
		var centerY:number=this.y;
		var tempScale:number=this.scaleX;
		this.scaleX=1;
		this.scaleY=1;
		this.width=this.width*tempScale;
		this.height=this.height*tempScale;
		this.anchorOffsetX = this.width/2;
        this.anchorOffsetY = this.height/2;
		this.x=centerX;
		this.y=centerY;
	}
	//获取初始大小和现在的缩放比例
	public getOriginalScale():number{
		return this.originalWidth/this.getRealWidth();
	}
	public getRealWidth():number{
		return this.scaleX*this.resizeWidth;
	}
	public getRealHeight():number{
		return this.scaleX*this.resizeHeight;
	}
	
	//图片移出边框调整图片的位置
	public adjustMove():void
	{
		var realWidth:number=this.getRealWidth();
		var realHeight:number=this.getRealHeight();
		if(this.x-realWidth/2>0){
			this.x=realWidth/2;
		} 
		if(this.x+realWidth/2<this.containerWidth){
			this.x=this.containerWidth-realWidth/2;
		} 
		if(this.y-realHeight/2>0){
			this.y=realHeight/2;
		}
		if(this.y+realHeight/2<this.containerHeight){
			this.y=this.containerHeight-realHeight/2;
		}
	}
}